package com.zllkj.mm.module.main;

import android.content.Context;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.zllkj.mm.R;
import com.zllkj.mm.base.BaseActivity;
import com.zllkj.mm.base.BaseAppFragment;
import com.zllkj.mm.common.L;
import com.zllkj.mm.model.entity.MsgListFragment;
import com.zllkj.mm.module.UIHelper;
import com.zllkj.mm.module.moment.PushPostFragment;
import com.zllkj.mm.module.home.fragments.SquareFragment;
import com.zllkj.mm.module.main.fragments.BlankFragment;
import com.zllkj.mm.module.main.fragments.MainFragment;
import com.zllkj.mm.module.main.fragments.MineFragment;
import com.zllkj.mm.util.EmptyUtils;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends BaseActivity {

    LinearLayout llTabBar;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    public static DrawerLayout drawerLayoutS;

    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        llTabBar = findViewById(R.id.llTabBar);
        context = this;

        switchTab(0);

        for (int i = 0; i < llTabBar.getChildCount(); i++) {
            int finalI = i;
            llTabBar.getChildAt(i).setOnClickListener(v -> {
                if (finalI != 0 && finalI != 1 && EmptyUtils.isEmpty(Hawk.get(L.token))) {
                    UIHelper.showLoginPage_notClean(this);
                    return;
                }
                for (int i1 = 0; i1 < llTabBar.getChildCount(); i1++) {
                    TextView childAt1 = (TextView) llTabBar.getChildAt(i1);
                   // childAt1.setTextColor(Color.parseColor(finalI == i1 ? "#0077c4" : "#999999"));
                }
                switchTab(finalI);
            });

        }

    }




    BaseAppFragment currentFragment;
    int id = 0;

    public void switchTab(int id) {
        this.id = id;
//        rlSearch.setVisibility(id == 0 ? View.VISIBLE : View.INVISIBLE);

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        currentFragment = null;
        switch (id) {
            case 0:
                currentFragment = MainFragment.newInstance();
                break;
            case 1:
                currentFragment = new SquareFragment(SquareFragment.Companion.getFRIEND_DATA_TYPE_DEFAULT());
                break;
            case 2:
                currentFragment = new PushPostFragment();
                break;
            case 3:
                currentFragment = new MsgListFragment();
                break;
            case 4:
                currentFragment = MineFragment.newInstance();
                break;
            default:
                break;
        }
        for (int i = 0; i < 5; i++) {
            Fragment fragment = fm.findFragmentByTag(i + "homepage");
            if (fragment != null) {
                ft.hide(fragment);
            }
        }
        String tag = id + "homepage";
        if (fm.findFragmentByTag(tag) != null) {
            ft.show(fm.findFragmentByTag(tag));
        } else {
            ft.add(R.id.flContainer, currentFragment, tag);
        }
        ft.commitAllowingStateLoss();
    }

}
